<template>
  <div class="address-detail-main">
    <el-dialog
      title="地址详情"
      :before-close="empty"
      :visible.sync="isOpen"
      width="60%"
    >
      <!-- form表单内容 -->
      <el-form
        ref="form"
        class="detail-form"
        :model="form"
        :rules="rules"
        label-width="150px"
        :inline="true"
      >
        <!-- rules根据 el-form-item的prop来判断规则 -->
        <el-form-item size="mini" prop="addressName" label="发货地址">
          <el-input
            v-model="form.addressName"
            placeholder="发货地址"
            class="inner-input"
            clearable
          />
        </el-form-item>
        <el-form-item size="mini" label="发货人姓名">
          <el-input
            v-model="form.name"
            class="inner-input"
            placeholder="发货人姓名"
            clearable
          />
        </el-form-item>
        <el-form-item size="mini" label="发货人手机号">
          <el-input
            v-model="form.phone"
            class="inner-input"
            placeholder="发货人手机号"
            clearable
          />
        </el-form-item>
        <el-form-item size="mini" label="邮政编码">
          <el-input
            v-model="form.postCode"
            class="inner-input"
            placeholder="邮政编码"
            clearable
          />
        </el-form-item>
        <el-form-item size="mini" label="收货地址">
          <el-cascader
            v-model="form.cityCode"
            class="inner-input"
            :options="options"
            @change="handleChange"
          />
        </el-form-item>
        <el-form-item size="mini" label="详细地址">
          <el-input
            v-model="form.detailAddress"
            class="inner-input"
            placeholder="详细地址"
            clearable
          />
        </el-form-item>
        <el-row :gutter="20">
          <el-col :span="12" :offset="0">
            <el-form-item size="mini" label="是否默认收货地址">
              <el-switch
                v-model="form.receiveStatus"
                :active-value="1"
                :inactive-value="0"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12" :offset="0">
            <el-form-item size="mini" label="是否默认发货地址">
              <el-switch
                v-model="form.sendStatus"
                :active-value="1"
                :inactive-value="0"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="isOpen = false;form={}">取 消</el-button>
        <el-button :loading="btnLoading" type="primary" @click="onSubmit"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { regionDataPlus, CodeToText } from 'element-china-area-data'
import addressApi from '@/api/order/address/index'
import mixin from '@/mixins/index'
export default {
  name: 'AddressDetai',
  mixins: [mixin],
  data() {
    return {
      btnLoading: false,
      isOpen: false,
      form: {
        addressName: '', // 地址名称 如:河南发货点
        name: '', // 发货人姓名
        phone: '',
        postCode: '', // 邮政编码
        cityCode: [], // 地址编码,英文逗号隔开,比如
        detailAddress: '', // 详细收货地址
        receiveStatus: 1,
        sendStatus: 1
      },
      rules: {
        addressName: [
          { required: true, message: '发货地址不能为空', trigger: 'blur' }
        ]
      },
      options: regionDataPlus, // 省市区三级联动data
      id: ''
    }
  },
  created() {},
  mounted() {},
  methods: {
    //点击关闭时的回调
    empty() {
      this.form = {}
     this.isOpen=false
    },
    openAddressDetail(id) {
      if (id) {
        this.id = id
        this.getAddressDetail()
      }
      this.isOpen = true
    },
    handleChange(v) {
      this.cityCode = v
    },
    getAddressDetail() {
      addressApi.addressDetail(this.id).then((res) => {
        if (res.success) {
          this.form = {
            ...res.data.address,
            cityCode: res.data.address.cityCode.split(',')
          }
        }
      })
    },
    onSubmit() {
      this.btnLoading = true
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.id) {
            const data = {
              ...this.form,
              cityCode: this.form.cityCode.join(','),
              province: CodeToText[this.cityCode[0]],
              city: CodeToText[this.cityCode[1]],
              region: CodeToText[this.cityCode[2]]
            }
            addressApi.updateAddress(data).then((res) => {
              if (res.success) {
                this.isOpen = false
                this.btnLoading = false
                this.$emit('success')
              } else {
                this.btnLoading = false
                this.$message(res.message)
              }
            })
          } else {
            const data = {
              ...this.form,
              cityCode: this.form.cityCode.join(','),
              province: CodeToText[this.cityCode[0]],
              city: CodeToText[this.cityCode[1]],
              region: CodeToText[this.cityCode[2]]
            }
            addressApi.saveAddress(data).then((res) => {
              if (res.success) {
                this.isOpen = false
                this.btnLoading = false
                this.$emit('success')
              } else {
                this.btnLoading = false
                this.$message(res.message)
              }
            })
          }
        } else {
          this.$message.error('请填写必要信息')
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">
.address-detail-main {
  .detail-form {
    .inner-input {
      width: 300px;
    }
  }
}
</style>